package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.base.CurrentUser;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.system.sys.model.dto.SysPointSiteDto;
import com.yungu.swift.system.sys.model.dto.SysWayDto;
import com.yungu.swift.system.sys.model.param.*;
import com.yungu.swift.system.sys.model.vo.AdminSystemPointSiteVo;
import com.yungu.swift.system.sys.model.vo.SimpleWayInfo;
import com.yungu.swift.system.sys.service.SysPointSiteService;
import com.yungu.swift.system.sys.service.SysWayService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author : cuixiuyin
 * @date : 2019/6/17
 */
@RestController
@RequestMapping("/admin/v1/system/way")
@Api(value = "【系统服务-业务设置中心】", tags = "跨城路线设置")
public class WayAdmin {

    @Reference
    private SysPointSiteService sysPointSiteService;

    @Reference
    private SysWayService sysWayService;

    @RequestMapping(value = "/routeOriginCitys", method = RequestMethod.GET)
    @ApiOperation(value = "获取路线起点城市", httpMethod = "GET")
    public ResponseData<Set> listOriginCity(@RequestBody AdminParam adminParam) {
        //获取运营起点
        Map<String, Object> map = MapUtils.build();
        map.put("appid", adminParam.getAppid());
        map.put("agentUuid", adminParam.getUserAgentUuid());
        map.put("companyUuid", adminParam.getUserCompanyUuid());
        map.put("state", CommonConstant.STATE_NORMAL);
        ResponseData<List<SysPointSiteDto>> responseData = sysPointSiteService.findOrigins(map);
        if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
            List<SysPointSiteDto> pointSiteDtos = responseData.getData();
            Set<String> collect = pointSiteDtos.stream().map(pointSiteDto -> pointSiteDto.getCityName())
                    .collect(Collectors.toSet());
            return ResponseData.buildSuccessResponse("获取线路城市数据成功", collect);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取线路城市数据失败");
    }

    @RequestMapping(value = "/siteOriginCitys", method = RequestMethod.GET)
    @ApiOperation(value = "获取起终点起点城市", httpMethod = "GET")
    public ResponseData<Set> siteOriginCitys(@RequestBody AdminParam adminParam) {
        //获取运营起点
        Map<String, Object> map = MapUtils.build();
        map.put("appid", adminParam.getAppid());
        map.put("agentUuid", adminParam.getUserAgentUuid());
        map.put("companyUuid", adminParam.getUserCompanyUuid());
        map.put("state", CommonConstant.STATE_NORMAL);
        ResponseData<List<SysPointSiteDto>> responseData = sysPointSiteService.list(map);
        if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
            List<SysPointSiteDto> origins = responseData.getData();
            Set<String> collect = origins.stream().map(origin -> origin.getCityName()).collect(Collectors.toSet());
            return ResponseData.buildSuccessResponse("获取起终点城市数据成功", collect);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取起终点数据失败");
    }


    @RequestMapping(value = "/routePageData", method = RequestMethod.POST)
    @ApiOperation(value = "获取路线管理分页数据", httpMethod = "POST")
    public ResponseData<PageVo<SysWayDto>> routePageData(@RequestBody AdminWayPageParam wayPageParam) {
        wayPageParam.setAdminAgentUuid(wayPageParam.getUserAgentUuid());
        return sysWayService.findWithOriginDestByPage(wayPageParam);
    }

    @RequestMapping(value = "/sitePageData", method = RequestMethod.POST)
    @ApiOperation(value = "获取起终点管理分页数据", httpMethod = "POST")
    public ResponseData<PageVo<AdminSystemPointSiteVo>> sitePageData(@RequestBody AdminWayPageParam wayPageParam) {
        return sysPointSiteService.queryPage(wayPageParam);
    }


    @RequestMapping(value = "/routeSaveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "跨城路线新增或者更新", httpMethod = "POST")
    public ResponseData routeSaveOrUpdate(@RequestBody AdminRouteParam routeParam) {
        if (routeParam.getOriginSiteUuid().equals(routeParam.getDestSiteUuid())) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "操作失败：起点与终点相同");
        }
        return sysWayService.saveOrUpdate(routeParam);
    }

    @RequestMapping(value = "/siteSaveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "跨城起终点新增或者更新", httpMethod = "POST")
    public ResponseData<Map<String, Object>> siteSaveOrUpdate(@RequestBody AdminSiteParam siteParam) {
        SysPointSiteDto sysPointSiteDto = new SysPointSiteDto();
        BeanUtils.copyProperties(siteParam, sysPointSiteDto);
        sysPointSiteDto.setAgentUuid(siteParam.getUserAgentUuid());
        sysPointSiteDto.setCompanyUuid(siteParam.getUserAgentUuid());
        String uuid = siteParam.getUuid();
        if (StringUtils.isNotEmpty(uuid)) {
            sysPointSiteDto.setUpdateBy(siteParam.getUserUuid());
        } else {
            sysPointSiteDto.setCreateBy(siteParam.getUserUuid());
        }
        return sysPointSiteService.saveOrUpdate(sysPointSiteDto, siteParam.getFenceList());
    }

    @RequestMapping(value = "/saveParcelRule", method = RequestMethod.POST)
    @ApiOperation(value = "保存跨城小件计价规则", httpMethod = "POST")
    public ResponseData<Boolean> saveParcelRule(@RequestBody AdminWayParcelParam wayParcelParam) {
        return sysWayService.saveParcelRule(wayParcelParam);
    }

    @RequestMapping(value = "/changeSite", method = RequestMethod.POST)
    @ApiOperation(value = "修改起终点状态", httpMethod = "POST")
    public ResponseData<Boolean> changePointSite(@RequestBody AdminSiteEditParam siteEditParam) {
        if (ParamUtil.isIllegal(siteEditParam.getUuid())) {
            AdminErrorEnum paramError = AdminErrorEnum.PARAM_ERROR;
            return ResponseData.buildErrorResponse(paramError.getCode(), paramError.getMsg());
        }
        if (siteEditParam.getState() != null) {
            return sysPointSiteService.updateState(siteEditParam.getUuid(), siteEditParam.getState());
        }
        if (siteEditParam.getStatus() != null) {
            return sysPointSiteService.updateStatus(siteEditParam.getUuid(), siteEditParam.getStatus());
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "服务器异常");
    }

    @RequestMapping(value = "/changeRoute", method = RequestMethod.POST)
    @ApiOperation(value = "修改路线状态", httpMethod = "POST")
    public ResponseData<Boolean> changeRoute(@RequestBody AdminRouteEditParam routeEditParam) {
        if (ParamUtil.isIllegal(routeEditParam.getUuid())) {
            AdminErrorEnum paramError = AdminErrorEnum.PARAM_ERROR;
            return ResponseData.buildErrorResponse(paramError.getCode(), paramError.getMsg());
        }
        if (routeEditParam.getState() != null) {
            return sysWayService.updateState(routeEditParam.getAppid(), routeEditParam.getUuid(), routeEditParam.getState());
        }
        if (routeEditParam.getStatus() != null) {
            return sysWayService.updateStatus(routeEditParam.getUuid(), routeEditParam.getStatus(), routeEditParam.getBusinessType());
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "服务器异常");
    }


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "获取拼车线路", httpMethod = "POST")
    public ResponseData<List<SysWayDto>> list(@CurrentUser AdminParam adminParam) {
        Map<String, Object> objectMap = MapUtils.convertObjToMap(adminParam);
        objectMap.put("agentUuid", adminParam.getUserAgentUuid());
        objectMap.put("companyUuid", adminParam.getUserCompanyUuid());
        return sysWayService.findWithOriginDest(objectMap);
    }

    @RequestMapping(value = "/origins", method = RequestMethod.POST)
    @ApiOperation(value = "获取拼车线路起点", httpMethod = "POST")
    public ResponseData<List<SysPointSiteDto>> origins(@RequestBody AdminWayOriginParam originParam) {
        return sysWayService.waysOriginByPoint(originParam.getAppid(), null, null, 0, originParam.getBusinessType(), originParam.getOrderDemand(), originParam.getTransferType());
    }

    @RequestMapping(value = "/dests", method = RequestMethod.POST)
    @ApiOperation(value = "获取拼车线路终点", httpMethod = "POST")
    public ResponseData<List<SysPointSiteDto>> dests(@RequestBody AdminWayDestParam waysDestParam) {
        if (ParamUtil.isIllegal(waysDestParam.getOriginUuid())) {
            AdminErrorEnum paramError = AdminErrorEnum.PARAM_ERROR;
            return ResponseData.buildErrorResponse(paramError.getCode(), paramError.getMsg());
        }
        return sysWayService.waysDestByOrigin(waysDestParam.getAppid(), waysDestParam.getOriginUuid(), waysDestParam.getOrderDemand());
    }

    @RequestMapping(value = "/fuzzyQueryByName", method = RequestMethod.POST)
    @ApiOperation(value = "线路信息模糊查询", httpMethod = "POST")
    public ResponseData<List<SimpleWayInfo>> fuzzyQueryByName(@RequestBody Map<String, String> map) {
        return sysWayService.fuzzyQueryByName(map.get("wayName"));
    }
}
